package com.woniuxy.run.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.ResponseResult;
import com.woniuxy.run.user.entity.Cart;
import com.woniuxy.run.user.mapper.CartMapper;
import com.woniuxy.run.user.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private CartService cartService;

    //添加购物车
    @RequestMapping("/add")
    public ResponseResult addCart(Cart cart){
        if(cartMapper.insert(cart)>0){
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.ERR;
    }

    //查看某个用户的购物车
    @GetMapping("/list")
    public ResponseResult<List<Cart>> queryCart(int userid){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",userid);
        List<Cart> carts = cartMapper.selectList(wrapper);
        ResponseResult<List<Cart>> result = new ResponseResult<>();
        result.setData(carts);
        result.setCode(200);
        return result;
    }

    //删除购物车中的商品  用户id、菜品id
    @GetMapping("/del")
    public ResponseResult del(@RequestParam int userid,@RequestParam List<Integer> dishids){
        if(cartService.del(userid, dishids)){
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.ERR;
    }









    //
    @GetMapping("/all")
    public ResponseResult<List<Cart>> all(){
        ResponseResult<List<Cart>> result = new ResponseResult<>();
        result.setData(cartService.all());
        return result;
    }
 }

